package com.cc.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyFilter implements Filter{

	private FilterConfig filterConfig;
	
	public MyFilter() {
		super();
	}
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse)response;
		String encode = filterConfig.getInitParameter("encode");
		req.setCharacterEncoding(encode);
		resp.setCharacterEncoding(encode);
		String basePath = req.getServletContext().getContextPath();
		req.getSession().setAttribute("basePath", basePath);
		filterChain.doFilter(request, response);
	}

}
